<?php

namespace app\common\model;

use think\Model;
use think\Db;

class Comment extends Model {

    private $child = array();

    /**
     * 传递文章id获取树状结构的评论
     * @param  int $id 可以是文章id 商品id==
     * @return array    树状结构数组
     */
    public function getChildData($id, $type = 1, $limit = 10) {
        $where = array(
            'c.article_id' => $id,
            'c.pid' => 0,
            'c.type' => $type
        );

        // 关联第三方用户表获取一级评论
        $data = Db::name('Comment')->alias('c')->field('c.*,u.user_id,u.user_name,u.nick_name,u.user_head')->join('__USERS__ u', 'c.user_id=u.user_id', 'left')->where($where)->limit($limit)->order('addtime desc')->select();

        foreach ($data as $k => $v) {

            $data[$k]['like_collect_count'] = Db::name('likeCollect')->where(['comment_id' => $v['id'], 'type' => 1])->count();
            $data[$k]['is_czan'] = Db::name('likeCollect')->field('id')->where(['comment_id' => $v['id'], 'type' => 1, 'user_id' => session('user_info.user_id')])->find();

            // 获取二级评论
            $this->child = array();
            $this->getTree($v);
            $child = $this->child;

            if (!empty($child)) {
                // 按评论时间asc排序
                $child = \UtilD::sortArrByField($child, 'addtime');
                foreach ($child as $m => $n) {

                    // 获取被评论人id
                    $reply_user_id = Db::name('Comment')->where(['id' => $n['pid']])->value('user_id');

                    // 获取被评论人昵称
                    $reply_user_info = Db::name('Users')->where(['user_id' => $reply_user_id])->find();
                    
                    $child[$m]['reply_name'] = $reply_user_info['nick_name'] ? $reply_user_info['nick_name'] : $reply_user_info['user_name'];
                }
            }
            $data[$k]['child'] = $child;
        }
        
        return $data;
    }

    // 递归获取树状结构
    public function getTree($data) {

        $where = array(
            'c.pid' => $data['id'],
        );

        $child = Db::name('Comment')->alias('c')->field('c.*,u.user_id,u.user_name,u.nick_name,u.user_head')->join('__USERS__ u', 'c.user_id=u.user_id', 'left')->where($where)->select();

        if (!empty($child)) {
            foreach ($child as $k => $v) {
                $v['like_collect_count'] = Db::name('likeCollect')->where(['comment_id' => $v['id'], 'type' => 1])->count();
                $v['is_czan'] = Db::name('likeCollect')->field('id')->where(['comment_id' => $v['id'], 'type' => 1, 'user_id' => session('user_info.user_id')])->find();

                $this->child[] = $v;
                $this->getTree($v);
            }
        }
    }

    /**
     * 获取评论内容和评论人的基本信息
     * @param type $article_id
     * @param type $type
     * @param type $limit
     * @return boolean
     */
    function getCommentList($article_id, $type = 1, $limit = 10) {
        if (!$article_id)
            return false;

        $data = Db::name('Comment')->alias('c')->field('c.*,u.user_id,u.user_name,u.nick_name,u.user_head')->join('__USERS__ u', 'c.user_id=u.user_id', 'left')->where(['c.article_id' => $article_id, 'c.type' => $type, 'c.status' => 1, 'c.pid' => 0])->limit($limit)->order('addtime desc')->select();

        if ($data) {
            foreach ($data as $k => $v) {
                $data[$k]['content'] = ubbReplace($v['content']);
                $data[$k]['like_collect_count'] = Db::name('likeCollect')->where(['comment_id' => $v['id'], 'type' => 1])->count();
                $data[$k]['is_czan'] = Db::name('likeCollect')->field('id')->where(['comment_id' => $v['id'], 'type' => 1, 'user_id' => session('user_info.user_id')])->find();
                $data[$k]['child'] = Db::name('Comment')->alias('c')->field('c.*,u.user_id,u.user_name,u.nick_name,u.user_head')->join('__USERS__ u', 'c.user_id=u.user_id', 'left')->where(['c.status' => 1, 'c.pid' => $v['id']])->select();
                if ($data[$k]['child']) {
                    foreach ($data[$k]['child'] as $kk => $vv) {
                        $data[$k]['child'][$kk]['content'] = ubbReplace($vv['content']);
                    }
                }
            }

            return $data;
        }
    }

}
